/*
 * KitchenService.java
 * Copyright (c) 2014, 2019, Kiri. 
 * All rights reserved.
 */
package com.onesalt.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

import com.onesalt.common.ExceptionConstants;
import com.onesalt.domain.Dishes;
import com.onesalt.domain.Kitchen;

/**
 * KitchenService
 *
 * @author kiri
 * @date 2015年4月6日
 * @version 1.0
 */
@Service
@Transactional
public class KitchenService extends AbstractService {

	private final KitchenRepository kitchenRepository;

	private final DishesRepository dishesRepository;

	@Autowired
	public KitchenService(KitchenRepository kitchenRepository, DishesRepository dishesRepository) {
		this.kitchenRepository = kitchenRepository;
		this.dishesRepository = dishesRepository;
	}

	public KitchenRepository getKitchenRepository() {
		return kitchenRepository;
	}

	public DishesRepository getDishesRepository() {
		return dishesRepository;
	}

	public Page<Kitchen> findAll(int page, int size) {
		return getKitchenRepository().findAll(new PageRequest(page, size));
	}

	public Page<Kitchen> findAll(Long cookId, String cookSn, String cookName, String sn, String name, String city, String address,
			Double startSpend, Integer endSpend, Date startApplyDate, Date endApplyDate, Boolean hasDishes, Boolean hasDinner,
			String notes, int page, int size) {
		List<SearchFilter> filters = new ArrayList<SearchFilter>();
		if (cookId != null) {
			filters.add(new SearchFilter("cook.cookId", Operator.EQ, cookId));
		}
		if (StringUtils.hasText(cookSn)) {
			filters.add(new SearchFilter("cook.user.sn", Operator.LIKE, cookSn));
		}
		if (StringUtils.hasText(cookName)) {
			filters.add(new SearchFilter("cook.user.name", Operator.LIKE, cookName));
		}
		if (StringUtils.hasText(sn)) {
			filters.add(new SearchFilter("sn", Operator.LIKE, sn));
		}
		if (StringUtils.hasText(name)) {
			filters.add(new SearchFilter("name", Operator.LIKE, name));
		}
		if (StringUtils.hasText(city)) {
			filters.add(new SearchFilter("city", Operator.LIKE, city));
		}
		if (StringUtils.hasText(address)) {
			filters.add(new SearchFilter("address", Operator.LIKE, address));
		}
		if (startSpend != null) {
			filters.add(new SearchFilter("averageSpend", Operator.GTE, startSpend));
		}
		if (endSpend != null) {
			filters.add(new SearchFilter("averageSpend", Operator.LT, endSpend));
		}
		if (startApplyDate != null) {
			filters.add(new SearchFilter("cook.applyTime", Operator.GTE, startApplyDate));
		}
		if (endApplyDate != null) {
			filters.add(new SearchFilter("cook.applyTime", Operator.LT, endApplyDate));
		}
		if (StringUtils.hasText(notes)) {
			filters.add(new SearchFilter("notes", Operator.LIKE, notes));
		}

		if (filters.size() == 0) {
			return findAll(page, size);
		}
		return getKitchenRepository().findAll(DynamicSpecifications.bySearchFilter(filters, Kitchen.class), new PageRequest(page, size));
	}

	public List<String> findAllCities() {
		return getKitchenRepository().findAllCities();
	}

	public Kitchen syncDishes(long kitchenId, List<Dishes> dishes) {
		Kitchen kitchen = getKitchenRepository().findOne(kitchenId);
		if (kitchen == null) {
			throw ExceptionConstants.KITCHEN_NOT_EXISTS_EXCEPTION;
		}

		List<Dishes> src = new ArrayList<Dishes>(kitchen.getDishes());
		List<Dishes> all = new ArrayList<Dishes>();
		List<Long> idList = new ArrayList<Long>();
		for (Dishes obj : dishes) {
			if (obj.getDishesId() == 0L) {
				obj.setCreateTime(new Date());
			}
			else {
				idList.add(obj.getDishesId());
			}
			all.add(getDishesRepository().save(obj));
		}

		for (Dishes d : src) {
			if (!idList.contains(d.getDishesId())) {
				getDishesRepository().delete(d.getDishesId());
			}
		}
		kitchen.setDishes(all);
		getKitchenRepository().save(kitchen);
		return getKitchenRepository().findOne(kitchenId);
	}
}
